<template>
<div>
  <div class="search">
    <input v-model="keyword" class="search-input" type="text" placeholder="请输入城市名或拼音" />
  </div>
  <div class="search-content" ref='search' v-show='keyword'>
    <ul>
      <li class="search-item" v-for='item in list' :key='item.id'>
        {{item.name}}
      </li>
      <li class="search-item" v-show='hasNoData'>
        未匹配到数据
      </li>
    </ul>
  </div>
</div>
</template>
<script>
import Bscroll from 'better-scroll'
export default {
  name: "search",
  props: {
    cities:Object
  },
  data () {
    return {
      keyword:'',
      list:[],
      timer:null
    }
  },
  computed: {
    hasNoData(){
      return !this.list.length;
    }
  },
  watch: {
    keyword(){
      if(this.timer){
        clearTimeout(this.timer);
      };
      this.timer = setTimeout(()=>{
        const result = [];
        for(let i in this.cities){
          this.cities[i].forEach((res)=>{
            if(res.spell.indexOf(this.keyword) > -1 || res.name.indexOf(this.keyword) > -1){
              result.push(res);
            }
          })
        };
        console.log('121212',result)
        this.list = result;
      },100) 
    }
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.search)
  }
};
</script>
<style lang="stylus" scoped>
@import '~@/assets/common/commonstyl/common.styl';

.search {
    height :72px;
    padding:0 10px 3px;
    background-color:$bgColor;
  .search-input {
      width :100%;
      height :62px;
      line-height :62px;
      box-sizing :border-box;
      padding:0 10px;
      text-align :center;
      color :#666;
      border-radius :15px;

  }
}
.search-content{
  z-index :99;
  overflow :hidden;
  position:absolute;
  top:155px;
  left :0;
  right :0;
  bottom :0;
  background-color :#ccc;
  .search-item{
    line-height :62px;
    padding-left :20px;
    background-color :#fff;
    color :#666;
  }
}
</style>